<?php

namespace app\oauth\controller;

use app\BaseController;
use think\facade\View;

use function PHPSTORM_META\type;

class My extends BaseController
{

    public function index()
    {
        $state = "aaabbb";


        $query = http_build_query([
            'response_type' => 'code',
            'client_id' => '10003',
            'redirect_uri' => 'http://127.1.1.226/oauth/my/callback',
            'scope' => 'email',
            'state' => $state,
        ]);

        // return redirect('http://127.1.1.226/index/oauth/authorize/?' . $query);

        $url = 'http://127.1.1.226/oauth/oauth/authorize/?' . $query;

        View::assign('url', $url);

        return View::fetch();
    }



    //刷新访问令牌(access token)
    public function refresh()
    {

        $refresh_token = "def5020021a33e132fb2b13b00686d69b8d1c332ee58a875a01ae7aeaf32e9a2a6953702aa513bfa5ebe0af9f543d20b10584f2bd35be6267490e187233093f8aeb4ebe9619fbbd042ae5cbaa2f81877146929430f2c11634f218da7d6585795e26e0165b69110b383ed0c71f4b4d40917588b12a25af2dc82c50df7072ef88a5162552656df3bb95f11a4c09a078d2b4afe50a1867a5717a819cb6562113a92a9d73ec994237c61f9c6b2de33ec56d856d37301bf1aaffc320c7e09e7e6c06a29e5d163e7d41437d17d9692785037245a75bfea3f35ac5f175d1aa51ac8de2acf3545aa22a3f7d1e03e32c935f843cee9d43c6fd8656d7ac1fd09d47ac26966304188cd3f40ce201e40d68350d63393b5c6e107b53f8ab14997c02c7e07cc599af3bd63b3f958bfa8890de10617dbab070d24a0908017fe2b7a6a7a007e7f3d95fd553e8731b62d13f14cb855f89e917c0f82936df4bfaf5445be6e9227d91fe2d28b6fdd";
        


        $http = new \GuzzleHttp\Client;

        $response = $http->post('http://127.1.1.226/oauth/oauth/refresh/', [
            'form_params' => [
                'grant_type' => 'refresh_token', // 注意这里是 refresh_token
                'refresh_token' => $refresh_token,
                'client_id' => '10003',
                'client_secret' => 'hxl123456',
                'scope' => '',
            ],
        ]);



        return (string)$response->getBody();
    }



    // 客服端授权
    public function client()
    {
        $http = new \GuzzleHttp\Client;

        $response = $http->post('http://127.1.1.226/oauth/oauth/client', [
            'form_params' => [
                'grant_type' => 'client_credentials',
                'client_id' => '10003',
                'client_secret' => 'hxl123456',
                // 'scope' => '',
            ],
        ]);



        return (string)$response->getBody();

        $res = json_decode($response->getBody(), true);


        $token_type = $res['token_type'];
        $access_token = $res['access_token'];

        dump($res);


        // $this->send_t($access_token);

    }


    public function callback()
    {

        dump("这是回调页面");

        $code = $this->request->param('code');
        $state = $this->request->param('state');

        if (!$code) {

            $access_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJteWF3ZXNvbWVhcHAiLCJqdGkiOiI3Nzc3YTZmNmQ1N2M5N2Q4NjY0MDM2ZDQwOThkNmU4YjI0ZGJkNGRkMDU3MDUxNGY3ZjFkYjFlOGFkYTZjM2M2MzJlOGNiMmMwY2Q0MTViNCIsImlhdCI6MTcyMDM0NDQ1Ni4wNDgyMjYsIm5iZiI6MTcyMDM0NDQ1Ni4wNDgyMzIsImV4cCI6MTcyMDM0ODA1Ni4wMzkxNzIsInN1YiI6IjEiLCJzY29wZXMiOlsiZW1haWwiXX0.ZYz_VJ5wMjR8vVAmOEQaMJs-1evoQr8HbZfq6VD827crGi_7wcCxS7KM9AEgigQmrDp-gh9HvcNUk2V_AuQCJAjjxckdhOhcpCcctQMe3mIgGruGvP7rKOcxfhDBaiz0uA-nf5h5_xbHDnDkobctmjgCwV7KCxNz4cqpakDwK6Rbb4nADGoLITF7vcp5Fsr5t0p1naEraZlhVorydubcWB6lJ7buRlhDdNUSP8YDEbOThBh3LzFchGgsCwMFFEE0mbA9eXwIn6p-QQG_iPRiNpieE49Q-_aPDvB_zwSp83vKDg3LRxPhx65h1qINjx_fq8euJijKL7BUQbtCcucYfQ';
            dump("隐式模式");
            $this->send_t($access_token);
            exit();
        }


        //如果有已有token

        //用code换取token
        $http = new \GuzzleHttp\Client;
        $response = $http->post('http://127.1.1.226/oauth/oauth/issueToken/', [
            'form_params' => [
                'grant_type' => 'authorization_code',
                'client_id' => '10003',
                'client_secret' => 'hxl123456',
                'redirect_uri' => 'http://127.1.1.226/oauth/my/callback',
                'code' => $this->request->get("code"), // 批准请求后服务端颁发的授权码
            ],
        ]);


        $res = json_decode($response->getBody(), true);


        $token_type = $res['token_type'];
        $access_token = $res['access_token'];

        dump($res);




        $this->send_t($access_token);





        // return (string)$response->getBody();
    }




    //授权码模式请求
    public function authorize()
    {

        $state = "aaabbb";


        $query = http_build_query([
            'response_type' => 'code',
            'client_id' => '10003',
            'redirect_uri' => 'http://127.1.1.226/oauth/my/callback',
            'scope' => '',
            'state' => $state,
        ]);

        return redirect('http://127.1.1.226/oauth/oauth/authorize/?' . $query);
    }


    //用token直接请求
    public function user()
    {
        $http = new \GuzzleHttp\Client;
        $access_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxMDAwMyIsImp0aSI6IjRmMTU1MjA3MTlmMmQxMWE1ZDdmYThmODRmMGNiZjQ1NjNiOGZjNTZlYTQwZWEyZTgwOTYwYzQ4OWVjYjI2ZDQ0ZDAyNzdlNjNmY2Y0OTJjIiwiaWF0IjoxNzIwNDkxOTg3LjM1Njg4OCwibmJmIjoxNzIwNDkxOTg3LjM1Njg5MywiZXhwIjoxNzIwNDk1NTg3LjM1MDU1OCwic3ViIjoiIiwic2NvcGVzIjpbImVtYWlsIl19.Cl7HWRp_MTLSdx-sawabsHlczKG3g4ZKgYaJVKXMm-XwqIQL8d3ildaoKq7Z7I5UyzAwJhKnlXeJ0CuCe3gRajk49z-U1xJQZlANc_KfLyponEwepqlUWhtW8kMvNyrg0rmrMlG-bhtOM-EeCx0vQIfzd2fDW-nut31s-YiKMgull_C21a7a-aXD9aPt-JxOERvqZpIip5PBflGM1r5PGwn_qxroAEYF3cGuZ95qEUWhbxgJB2nEbzzDhAu0cvS5Wwyf6z8tPxbwR7djM4P0gSD6S3hBsjTkxfht0mv14KvzR7t0--xxzXwmp-J8iC-7COEf8B-hInIz1Zyy0OU8Tw';

        $response = $http->request('GET', 'http://127.1.1.226/oauth/api/user', [
            'headers' => [
                'Accept' => 'application/json',
                'Authorization' => 'Bearer ' . $access_token, // 注意 Bearer 后带一个空格
            ],
            'form_params' => [
                'scopes' => 'email',

            ],
        ]);
        dump((string)$response->getBody());
    }


    //隐式授权模式
    public function implicit()
    {
        $state = "implicit_cc";


        $query = http_build_query([
            'response_type' => 'token',
            'client_id' => '10003',
            'redirect_uri' => 'http://127.1.1.226/oauth/my/callback',
            'scope' => '',
            'state' => $state,
        ]);

        return redirect('http://127.1.1.226/oauth/oauth/implicit/?' . $query);
    }

    //密码模式获取token
    public function password()
    {



        $http = new \GuzzleHttp\Client;

        // $response = $http->post('http://127.1.1.226/oauth/oauth/password/', [
        //     'form_params' => [
        //         'grant_type' => 'password', // 注意这里是 password
        //         'client_id' => '10003',
        //         'client_secret' => 'hxl123456',
        //         'username' => 'hxl95810', // 用户名
        //         'password' => 'hxl123456', // 密码
        //         'scope' => '', // ''?* 代表所有权限
        //     ]
        // ]);

        $response = $http->post('http://127.1.1.226/oauth/oauth/password/', [
            'form_params' => [
                'grant_type' => 'password', // 注意这里是 password
                'client_id' => '10003',
                'client_secret' => 'hxl123456',
                'username' => 'air9987', // 用户名
                'password' => 'air123456', // 密码
                // 'scope' => 'email', // ''?* 代表所有权限
            ]
        ]);

        // return (string) $response->getBody();


        $res = json_decode($response->getBody(), true);

        $token_type = $res['token_type'];
        $access_token = $res['access_token'];

        dump($res);exit();

        $this->send_t($access_token);
    }


    function send_t($access_token)
    {
        $http = new \GuzzleHttp\Client;

        $response = $http->request('POST', 'http://127.1.1.226/oauth/api/user', [
            'headers' => [
                'Accept' => 'application/json',
                'Authorization' => 'Bearer ' . $access_token, // 注意 Bearer 后带一个空格
            ],
            'form_params' => ['scope' => 'email'],
        ]);

        dump((string)$response->getBody());
    }
}
